package com.example.demo.service;

import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @program: rabbit
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-05-25
 **/
@Service
public class UserService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    RedisTemplate redisTemplate;
    @Resource
    UserMapper userMapper;


    @RabbitListener(queues = "amqp.queue")
    public void getUser(User user){
        userMapper.addUser(user);
        System.out.println(user.toString() + "=" + "id是" + user.getId());
        redisTemplate.opsForValue().set(user.getId()+"",user,100, TimeUnit.SECONDS);
    }
    @Scheduled(cron = "0/5 * * * * ?")
    public void sendMsg(){
        rabbitTemplate.convertAndSend("amqp.exchange","amqp.haha",new User(1,"张晓明","江苏省南京市雨花台区"));
        System.out.println("遍历redis-start");
        userMapper.getUsers().stream().forEach((x)-> System.out.println(redisTemplate.opsForValue().get(x.getId()+"")));
        System.out.println("遍历redis-end");
    }
}
